//
//  ZNWBTitleButton.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/8/21.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import UIKit

class ZNWBTitleButton: UIButton {
    
    init(title: String?) {
        // 调用UIButton指定的构造函数
        super.init(frame: CGRect())
        
        if title == nil {
            // 如果没有指定的title，就设置为首页
            setTitle("首页", for: .normal)
        } else {
            // 让箭头与标题之间有空隙
            setTitle(title! + " ", for: .normal)
            
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: .normal)
            setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        
        // 设置大小
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
              let imageView = imageView
            
        else {
            return
        }
        
        // 让图片在右，标题在左
        // Swift中可以直接改变结构体中的值，OC中不行
        titleLabel.frame.origin.x = 0
        imageView.frame.origin.x = titleLabel.bounds.width
    }
}
